﻿using System;
using System.Collections.Generic;
using System.Linq;

using AMC.SubstitutionCache.Drivers;
using AMC.SubstitutionCache.Services;

using Orchard;

namespace AMC.SubstitutionCache.Rendering
{
    /// <summary>
    /// Provides rendering info for all drivers implementing ISubstitutionDriver. Used as a lookup during rendering
    /// </summary>
    public class SubstitutionDriverSelector : ISubstitutionDriverSelector, ISingletonDependency
    {
        private readonly ILookup<SubstitutionShapeRenderingInfo, SubstitutionDriverRenderingInfo> _drivers;

        public SubstitutionDriverSelector(IEnumerable<ISubstitutionDriver> drivers)
        {
            _drivers =
                drivers.SelectMany(
                    d => d.HandledShapes.Select(i => new Tuple<SubstitutionShapeRenderingInfo, SubstitutionDriverRenderingInfo>(i, i))).ToLookup(t => t.Item1, t => t.Item2);
        }

        public IEnumerable<SubstitutionDriverRenderingInfo> GetRenderingInfoForDrivers(SubstitutionContext context)
        {
            return _drivers[context];
        }
    }
}